<?php
/**
 * Created by PhpStorm.
 * User: liyang
 * Date: 2017/4/26
 * Time: 9:38
 */

namespace App\Models;

use App\Libs\Tool\Impl\QueryResult;
use App\Libs\Tool\Impl\ReqToQueryAndParamImpl;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Log;

/**
 * 基础模型，目前包括参数查询、获取参数、获取保存参数、获取更新参数
 *
 * Class BaseModel
 * @package App\Models
 */
class ExtBaseModel extends BaseModel
{

    /**
     * @var array
     */
    protected $step_fields = [];
    /**
     * @var array
     */
    protected $step_fields_default = [];
    /**
     * @var bool
     */
    protected $is_custom_timestamps = false;
    /**
     * @var bool
     */
    protected $is_custom_admin_id = false;

    /**
     * 启动步骤
     * @param $step
     * @param Request $request
     * @return bool
     */
    public function start_up_step($step, Request $request) {
        if(!$this->check_step($step)) {
            return false;
        }
        try {
            $save_param = $this->requestToArrayCustomSave(
                $this->step_fields[$step],
                $this->step_fields_default[$step],
                $request
            );
            return self::query()->insert([$save_param]);
        } catch (\Exception $e) {
            Log::error($e->getMessage());
            return false;
        }
    }

    /**
     * 检测step是否符合要求
     * @param int $step
     * @return bool
     */
    private function check_step($step) {
        if($step < 0 ||
            !is_array($this->step_fields) ||
            $step >= count($this->step_fields)) {
            return false;
        }
        return true;
    }

    /**
     * 获取请求中的基础对象参数，不包含created_at、updated_at字段
     * 如果获取不到数据，字段将会被设置为''字符串，0不再此行列
     *
     * @param array $step_field
     * @param array $step_fields_default
     * @param Request $request
     * @return array
     */
    public function requestToArrayCustom($step_field, $step_fields_default, Request $request) {
        $arr = [];
        foreach ($step_field as $item) {
            if(isEmpty($request->get($item, ''))) {
                try {
                    if(isEmpty($step_fields_default[$item])) {
                        $arr[$item] = '';
                    } else {
                        $arr[$item] = $step_fields_default[$item];
                    }
                } catch (\Exception $e) {
                    Log::error($e->getMessage());
                    $arr[$item] = '';
                }
            } else {
                $arr[$item] = $request->get($item, '');
            }

        }
        return $arr;
    }

    /**
     * @param array $step_field
     * @param array $step_fields_default
     * @param Request $request
     * @return array
     */
    public function requestToArrayCustomSave($step_field, $step_fields_default, Request $request) {
        $arr = $this->requestToArrayCustom($step_field, $step_fields_default, $request);
        if($this->is_custom_timestamps) {
            $time = currentDateTime();
            $arr['created_at'] = $time;
            $arr['updated_at'] = $time;
        }
        if($this->is_admin_id) {
            $arr['admin_id'] = getAdmin()->id;
        }
        return $arr;
    }

}
